package com.api.controller;

import com.api.domain.ApiAccount;
import com.api.domain.ImageDemo;
import com.api.service.IApiAccountService;
import com.api.service.IImageDemoService;
import com.common.annotation.PassToken;
import com.common.apiResult.ApiController;
import com.common.apiResult.ApiResult;
import com.common.apiResult.PageParam;
import com.common.exception.ApiException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName RedisCacheDemoController
 * @Description redis缓存使用演示操作
 * @Date 2020/10/12 16:51
 * Create By Renbowen
 */
@RestController
@RequestMapping(value = "/api/redis")
public class RedisCacheDemoController extends ApiController {

    @Autowired
    private IImageDemoService imageDemoService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @Cacheable(cacheNames = "accountCache")
    @PassToken
    public ApiResult redisList(PageParam pageParam, ImageDemo imageDemo){
        if (pageParam == null){
            pageParam = new PageParam();
        }
        PageHelper.startPage(pageParam.getPageNo(),pageParam.getPageSize());
        PageInfo<ImageDemo> pageInfo = new PageInfo<>(imageDemoService.selectImageDemoList(imageDemo));
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("total",pageInfo.getTotal());
        resultMap.put("list",pageInfo.getList());
        return ApiResult.ok(resultMap);
    }

    @RequestMapping(value = "/redisUpdate",method = RequestMethod.PUT)
    // @CacheEvict是清除缓存的注解  为了列表数据一直处于更新状态，我们需要在增删改上加这个主角
    // allEntries为true时，意思是说这个清除缓存是清除当前value值空间下的所有缓存数据。
    // beforeInvocation为true，意思是说当执行这个方法之前执行清除缓存的操作，这样不管这个方法执行成功与否，该缓存都将不存在。
    @CacheEvict(cacheNames = "accountCache", allEntries = true, beforeInvocation = true)
    @PassToken
    public ApiResult redisUpdate(ImageDemo imageDemo){
        int flag = imageDemoService.updateImageDemo(imageDemo);
        if (flag < 1){
            throw new ApiException("修改用户失败！");
        }
        return ApiResult.ok();
    }


    @RequestMapping(value = "/redisAdd",method = RequestMethod.POST)
    @CacheEvict(cacheNames = "accountCache", allEntries = true, beforeInvocation = true)
    @PassToken
    public ApiResult redisAdd(ImageDemo imageDemo){
        int flag = imageDemoService.insertImageDemo(imageDemo);
        if (flag < 1){
            throw new ApiException("新增用户失败！");
        }
        return ApiResult.ok();
    }


    @RequestMapping(value = "/redisDelete",method = RequestMethod.DELETE)
    @CacheEvict(cacheNames = "accountCache", allEntries = true, beforeInvocation = true)
    @PassToken
    public ApiResult redisAdd(Long id){
        int flag = imageDemoService.deleteImageDemoById(id);
        if (flag < 1){
            throw new ApiException("新增用户失败！");
        }
        return ApiResult.ok();
    }


    @RequestMapping(value = "/redisGetOne",method = RequestMethod.GET)
    @Cacheable(cacheNames = "accountCache")
    @PassToken
    public ApiResult redisGetOne(Long id){
        return ApiResult.ok(imageDemoService.selectImageDemoById(id));
    }






}
